﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
namespace Xwork.SEO.WebSite.Controllers
{
    /// <summary>
    /// 优化管理
    /// </summary>
    [UserAuthorize]
    public class SiteController : SEOController
    {
        //
        // GET: /Site/

        public ActionResult Index(int? pageIndex)
        {
            IPagedList<SeoSite> sites = db.SeoSites.OrderByDescending(p=>p.UserID).ToPagedList(pageIndex.HasValue ? pageIndex.Value : 1, 20);
        
            return View(sites);
        }

        /// <summary>
        /// 添加站点
        /// </summary>
        /// <returns></returns>
        public ActionResult AddSite(long? siteid)
        {
            
            return View();
        }
        [HttpPost]
        public ActionResult AddSite(SeoSite model)
        {
            model.UserID = this.User.ID;
            if (model.ID == 0)
            {
                db.SeoSites.Add(model);
                model.CreateTime = DateTime.Now;
            }
            else
            {
                db.SeoSites.Attach(model);
            }
            db.SaveChanges();
            return View(model);
        }


        public ActionResult Keywords(int? pageIndex, long? siteId)
        {
            IQueryable<SeoKeyword> q = db.SeoKeywords.Where(p => p.SeoSite.UserID == User.ID).OrderByDescending(p => p.ID);
            if (siteId.HasValue)
            {
                q = q.Where(p => p.SeoSiteID == siteId);
            }
            IPagedList<SeoKeyword> list = q.ToPagedList(pageIndex.HasValue ? pageIndex.Value : 1, 20);
            return View(list);
        }


        public ActionResult AddKeyword(long? siteid)
        {
            ViewData["engines"] = db.SearchEngines.ToList();
            ViewData["SeoSiteID"] = new SelectList(db.SeoSites.Where(p => p.UserID == User.ID).ToList(), "ID", "SiteUrl",siteid);
            return View(new SeoKeyword());
        }

        [HttpPost]
        public ActionResult AddKeyword(SeoKeyword model)
        {
            if (this.ModelState.IsValid)
            { 
            
            }
            if (model.SearchEngines == null || model.SearchEngines.Count == 0)
            {
                return Json(new { error = "请至少选择一个搜索引擎" });
            }

            SeoKeyword sk = new SeoKeyword()
            {
                KeywordName = model.KeywordName,
                IsAuthenticated = false,
                SeoSiteID = model.SeoSiteID,
                Price = 0,
                CreateTime = DateTime.Now
            };

            db.SeoKeywords.Add(sk);
            foreach (var item in model.SearchEngines)
            {
                sk.SearchEngines.Add(db.SearchEngines.Find(item.ID));
            }
            db.SaveChanges();
            return Json(new { message = "添加成功", url = Url.Action("keywords", "site") });
        }
        [HttpPost]
        public ActionResult DeleteKeyword(long keywordId)
        {
            SeoKeyword keyword = db.SeoKeywords.Find(keywordId);
            if (User.IsAdmin || User.ID == keyword.SeoSite.UserID)
            {
                keyword.Deleted = true;
                db.SaveChanges();
                return Json(new { message = "删除成功", url = Request.UrlReferrer });
            }
            return Json(new { error = "没有权限进行此操作" });
        }

        [HttpPost]
        public ActionResult DeleteSite(long siteid)
        {
            SeoSite site = db.SeoSites.Find(siteid);
            if (User.IsAdmin || User.ID == site.UserID)
            {
                site.Deleted = true;
                db.SaveChanges();
                return Json(new { message = "删除成功", url = Request.UrlReferrer });
            }
            return Json(new { error = "没有权限进行此操作" });
        }
    }
}
